/*
 * Copyright (C) 2007-2008 SlideMe client
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.slideme.android.manager.download.impl;

import android.content.Context;
import android.content.pm.PackageManager;
import org.slideme.android.manager.ContentHandler;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.URI;

/**
 * The handler uses an external process to install Android applications
 */
public class ExternalInstallerContentHandler implements ContentHandler {

    private Context context;

    public String getMimeType() {
        return "application/x-android-extern";
    }

    public void download(URI uri, String fileName, PackageManager packageManager) throws IOException {
        if (uri == null) {
            throw new IllegalArgumentException("ExternalContentHandler.download:uri");
        }
        OutputStream os = null;
        try {
            Socket socket = new Socket("10.0.2.2", 8090);
            os = socket.getOutputStream();
            os.write(uri.toString().getBytes());
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

    public void init(Context context) {
        if (context == null) {
            throw new IllegalArgumentException("context");
        }
        this.context = context;
    }
}
